# -*- coding: utf-8 -*-
import scrapy

from bytravel.items import BytravelItem
from copy import deepcopy

class SpecialtySpiderSpider(scrapy.Spider):
    name = 'specialty_spider'
    allowed_domains = ['bytravel.cn']
    start_urls = ['http://shop.bytravel.cn/']

    def parse(self, response):
        div_list = response.xpath("//div[@align='center']//div")

        for div in div_list:
            item = BytravelItem()
            if div.xpath("./@class").extract_first() == 'ht':
                item['pro_name'] = div.xpath(".//a/text()").extract_first().replace("特产", "")
                item['pro_url'] = "http://shop.bytravel.cn" + div.xpath(".//a/@href").extract_first()
                # yield province
                yield scrapy.Request(
                    item['pro_url'],
                    callback=self.parse_list_url,
                    meta={"item" : deepcopy(item)}
                )

    def parse_list_url(self, response):
        listmore_url = "http://shop.bytravel.cn/produce/" + response.xpath("//span[@class='listmore']/a/@href").extract_first()
        yield scrapy.Request(
            listmore_url,
            callback=self.parse_list,
            meta={"item" : deepcopy(response.meta['item'])}
        )

    def parse_list(self, response):
        item = response.meta['item']

        a_list = response.xpath("//a[@class='blue14b']")

        for a in a_list:
            item['name'] = a.xpath("./text()").extract_first()
            shorted_url = a.xpath("./@href").extract_first()
            item['url'] = "http://shop.bytravel.cn" + shorted_url
            item['img'] = response.xpath("//table[@id='tjtable']/tr/td/a[@href='"+shorted_url+"']/img/@src").extract_first()
            item['commont'] = response.xpath("//div[@id='tcjs']/a[@href='"+shorted_url+"']/../text()").extract_first().strip()
            yield scrapy.Request(
                item['url'],
                callback=self.parse_detail,
                meta={"item" : deepcopy(item)}
            )

        if '[下一页]' == response.xpath("//nav[@id='list-page']//li[last()]/a/text()").extract_first():
            yield scrapy.Request(
                "http://shop.bytravel.cn/produce/" + response.xpath(
                    "//nav[@id='list-page']//li[last()]/a/@href").extract_first(),
                callback=self.parse_list,
                meta={"item" : deepcopy(response.meta['item'])}
            )

    def parse_detail(self, response):
        item = response.meta["item"]
        item['img_details'] = response.xpath("//img[@class='hpic']/@src").extract_first()
        if item['img_details'] is None:
            item['img_details'] = response.xpath("//img[@class='hpic0']/@src").extract_first()
        content = ''
        for e in response.xpath("//div[@class='f14']//text()")[0:-1].extract():
            content += e.strip()
        item['content'] = content
        item['distinct'] = response.xpath("//div[@id='page_left']//a[last()]/text()").extract_first().replace("特产", "")
        self.logger.info(item)
        yield item
